# -*- coding:utf-8 -*-
'''
Created on 01/06/2011

@author: Hagut
'''
from loa_game import WHITE, BLACK, DIRECTIONS, EMPTY
from loa_game import LinesOfActionState, MoveAction, Direction
from our_adds.utils import PriorityQueue
from our_adds.euler_num import *

         

''' 
maybe we should remove all the calculations of new board,
and not return the new state. do we need it??
'''    
def getActionImproved(gameState, row, col, dir):
        '''
        Checks the validity and the ramifications of performing a move action at the given location in the given direction.
        
        @param row: The row coordinate.
        @param col: The column coordinate.
        @param direction: The direction to move in.
        @return: The action and resulting state or None if illegal.
        '''
        distance = 1
        first_enemy = gameState.size
        
        (i_row, i_col) = dir.move((row, col))
        while (0 <= i_row < gameState.size) and (0 <= i_col < gameState.size):
            cell = gameState.board[i_row][i_col]
            if cell != EMPTY:
                distance += 1
                if cell != gameState.getCurrentPlayer() and first_enemy == gameState.size:
                    first_enemy = max(abs(row - i_row), abs(col - i_col))
            (i_row, i_col) = dir.move((i_row, i_col))
        
        opposite_direction = Direction('temp', (-dir.delta[0], -dir.delta[1]))
        (i_row, i_col) = opposite_direction.move((row, col))
        while (0 <= i_row < gameState.size) and (0 <= i_col < gameState.size):
            if gameState.board[i_row][i_col] != EMPTY:
                distance += 1
            (i_row, i_col) = opposite_direction.move((i_row, i_col))
        #print row, col, distance
        #if distance == 1:
            #print dir, dir.delta
        if distance > first_enemy:
            return None, None
        
        ' updating the new location '
        (new_row, new_col) = (dir.delta[0]*distance + row, dir.delta[1]*distance + col)
        
        if not ((0 <= new_row < gameState.size) and (0 <= new_col < gameState.size)):
            return None, None
        
        new_cell = gameState.board[new_row][new_col]
        if new_cell == gameState.getCurrentPlayer():
            return None, None
        
        if new_cell == WHITE:
            new_whites = gameState.whites - 1
        else:
            new_whites = gameState.whites
        
        if new_cell == BLACK:
            new_blacks = gameState.blacks - 1
        else:
            new_blacks = gameState.blacks
        
        new_board = list(gameState.board)
        
        new_board[row] = list(new_board[row])
        new_board[row][col] = EMPTY
        new_board[row] = tuple(new_board[row])
        
        new_board[new_row] = list(new_board[new_row])
        new_board[new_row][new_col] = gameState.getCurrentPlayer()
        new_board[new_row] = tuple(new_board[new_row])
        
        ' !!!!!!!!!!! '
        ' update direction to contain correct delta '
#        dir.delta = (dir.delta[0]*distance, dir.delta[1]*distance)
        newDir = Direction(dir.name, (dir.delta[0]*distance, dir.delta[1]*distance))
        state = LinesOfActionState(gameState.size, gameState.turns_left - 1, tuple(new_board), new_whites, new_blacks)
        return (MoveAction(row, col, newDir), state)

def get1Successor(gameState):
    current_player = gameState.getCurrentPlayer()
#    if current_player == WHITE:
#        enemy = BLACK
#    else:
#        enemy = WHITE
    for row, row_data in enumerate(gameState.board):
        for col, cell in enumerate(row_data):
            if cell == current_player:
                for spin in [(row, col), (row - 1, col), (row, col - 1), (row - 1, col - 1)]:
                        action, state = gameState._getSpinAction(spin[0], spin[1])
                        if action is not None:
                            return action, state
                            
    